package com.veepeak.server.service;

import com.google.common.collect.Lists;
import com.veepeak.server.common.BaseRepository;
import com.veepeak.server.common.BaseService;
import com.veepeak.server.domain.UserVehicle;
import com.veepeak.server.domain.Vehicle;
import com.veepeak.server.repository.UserVehicleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class UserVehicleService extends BaseService<UserVehicle,String> {
    @Autowired
    private UserVehicleRepository userVehicleRepository;
    @Override
    public BaseRepository<UserVehicle, String> getRepo() {
        return this.userVehicleRepository;
    }

    public void deleteByUserIdAndVehicleId(String userId,String vehicleId){
        UserVehicle userVehicle = userVehicleRepository.getByUserIdAndVehicleId(userId, vehicleId);
        userVehicleRepository.delete(userVehicle);
    }

    public List<Vehicle> findByUserId(String userId) {
        List<UserVehicle> userVehicles = userVehicleRepository.findByUserId(userId);

        List<Vehicle> vehicles =
                userVehicles.stream().map(userVehicle -> userVehicle.getVehicle()).collect(Collectors.toList());

        return vehicles;
    }
}
